home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Standard File Samples / CustomPutAppend / CustomPutAppend.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-16  |  6.1 KB  |  178 lines  |  [TEXT/MMCC]

  1. //
  2. //    Application:    CustomPutAppend
  3. //    
  4. //    Description:    This demonstrates a CustomPutDialog with an Append button that
  5. //                    does not ask the user if he/she wants to replace the existing file.
  6. //                  the sample code in InsideMac:Files, and the Human Interface Guidelines
  7. //                    It also has Baloon Help strings for the Append button
  8. //
  9. //    Programmer:        David Hayward
  10. //                    Developer Technical Support
  11. //                    Apple Computer, Inc.
  12. //
  13. //    Environment:    Metrowerks C version 6, Universal Interfaces 2.0
  14. //
  15. //    History:        2/3/93
  16. //                      first draft
  17. //                    5/12/95
  18. //                      updated project for Metrowerks
  19. //    
  20.  
  21.  
  22. #include <QuickDraw.h>
  23. #include <Memory.h>
  24. #include <Fonts.h>
  25. #include <StandardFile.h>
  26. #include <Files.h>
  27. #include <Dialogs.h>
  28. #include <TextUtils.h>
  29.  
  30. #include "InitMac.h"
  31.  
  32.  
  33. /**\
  34. |**| ==============================================================================
  35. |**| DEFINES
  36. |**| ==============================================================================
  37. \**/
  38. #define    ReplyDialogID            128
  39. #define    ReplyDialogQuitItem        1
  40. #define    ReplyDialogAgainItem    2
  41.  
  42. #define    CustomPutAppendDialogID    129
  43. #define    sfItemAppendButton        13
  44.  
  45.  
  46. /**\
  47. |**| ==============================================================================
  48. |**| FUNCTION PROTOTYPES
  49. |**| ==============================================================================
  50. \**/
  51. short            MyAlert        ( StandardFileReply *reply ) ;
  52. pascal short    MyDlgHook    ( short item, DialogPtr theDialog, void *myDataPtr ) ;
  53.  
  54.                             
  55. /**\
  56. |**| ==============================================================================
  57. |**| FUNCTIONS
  58. |**| ==============================================================================
  59. \**/
  60.  
  61. /*------------------------------------------------------------------------------*\
  62.     MyDlgHook()
  63.  *------------------------------------------------------------------------------*
  64.         the dialog hook procedure responsible for activating and  
  65.         dimming the Append control and supressing the "Replace Existing?" 
  66.         dialog if the Append button is hit
  67. \*------------------------------------------------------------------------------*/
  68. pascal short MyDlgHook ( short item, DialogPtr theDialog, void *myDataPtr )
  69. {
  70.     static short        lastItem = 0;            /* remember lastItem so that we dont update    */
  71.                                                 /*  Append button on consecutive NullEvents    */
  72.     static short        which;                    /* remember if Save or Append was hit so we    */
  73.                                                 /*  can dismiss the "Replace" dialog if needed */
  74.     short                returnItem = item;        /* default returnItem is item */
  75.     long                refCon;
  76.     short                iType;
  77.     Handle                iHandle;
  78.     Rect                iRect;
  79.     FInfo                fndrInfo;
  80.     OSErr                err;
  81.     StandardFileReply    *sfr;
  82.  
  83.     refCon = GetWRefCon((WindowPtr)theDialog);    /* get the refCon of the current dialog */
  84.  
  85.     if (refCon == sfReplaceDialogRefCon)            /* if its the "Replace Existing?" dialog */
  86.         if (item == sfHookFirstCall)                /* and the dialog is just about to appear */
  87.             if (which == sfItemAppendButton)        /* and user last hit the append button */
  88.                 returnItem = 1;                        /* then hit the Replace button automatically */
  89.  
  90.     if (refCon == sfMainDialogRefCon)                /* if its the "Save As…" dialog */
  91.     {
  92.  
  93.         sfr = (StandardFileReply*)myDataPtr;        /* coerce myDataPtr to StandardFileReply */
  94.             
  95.         /* update Append button state... */
  96.         if ( (item == sfHookFirstCall) ||            /* if the dialog is just about to appear */
  97.              ( (item == sfHookNullEvent) &&            /* or if we just got a NullEvent */
  98.                (lastItem != sfHookNullEvent)))
  99.         {
  100.             GetDItem(theDialog,                        /* get the handle for the Append button */
  101.                      sfItemAppendButton,
  102.                      &iType, &iHandle, &iRect);
  103.             
  104.             /* GetFInfo with the current filespec to see it it exists */
  105.             err = FSpGetFInfo(&(sfr->sfFile), &fndrInfo);
  106.             
  107.             if ((err)    ||                            /* if file doesn't exist or */
  108.                 (fndrInfo.fdType != 'TEXT'))        /* isn't 'TEXT', then dim Append */
  109.                 HiliteControl((ControlHandle)iHandle, 255);
  110.             else                                    /* otherwise, activate Append Button */
  111.                 HiliteControl((ControlHandle)iHandle, 0);
  112.         }
  113.  
  114.  
  115.         if (item==sfItemOpenButton)                    /* if user hit Save button */
  116.             which = sfItemOpenButton;                /* then remember this is what was hit */
  117.  
  118.         if (item==sfItemAppendButton)                /* if user hit Append button */
  119.         {
  120.             which = sfItemAppendButton;                /* then remember this is what was hit */
  121.             sfr->sfReplacing |= 2;                    /* set 2nd bit so we we know it was append */
  122.             returnItem = sfItemOpenButton;            /* but perform a Save instead */
  123.         }
  124.     }
  125.     
  126.     lastItem = item;                                /* save lastItem */
  127.     return returnItem;
  128. }
  129.  
  130.  
  131. /*------------------------------------------------------------------------------*\
  132.     MyAlert()
  133.  *------------------------------------------------------------------------------*
  134.         output vital stats of the StandardFileReply to an Alert box
  135. \*------------------------------------------------------------------------------*/
  136. short MyAlert ( StandardFileReply *reply )
  137. {
  138.     Str255    goodStr, replStr;
  139.  
  140.     NumToString( (long)reply->sfGood, goodStr); 
  141.     NumToString( (long)reply->sfReplacing, replStr); 
  142.     ParamText(goodStr, replStr, reply->sfFile.name, nil);
  143.     return NoteAlert( ReplyDialogID, nil );
  144. }
  145.  
  146.  
  147. /*------------------------------------------------------------------------------*\
  148.     main()
  149.  *------------------------------------------------------------------------------*
  150.         initialize mamagers and the keep doing 
  151.         CustomPutFile until the user has had enough
  152. \*------------------------------------------------------------------------------*/
  153. void main ( void )
  154. {
  155.     StandardFileReply    reply;
  156.     Point                where = {-1,-1};            /* center dialog on main screen */
  157.     
  158.     InitToolBox(1) ;
  159.     
  160.     do
  161.     {
  162.         CustomPutFile(    "\pSave file as:",            /* the prompt string */
  163.                         "\pUntitled",                /* the default filename */
  164.                         &reply,                        /* the StandardFileReply structure */
  165.                         CustomPutAppendDialogID,    /* the dialog's ID */
  166.                         where,                        /* position of dialog on screen */
  167.                         (DlgHookYDProcPtr)MyDlgHook,/* the Dialog Hook procedure */
  168.                         nil,                        /* no modal dialog filterProc */
  169.                         nil,                        /* no activeListPtr */
  170.                         nil,                        /* no activateProc */
  171.                         &reply                        /* yourDataPtr: pass reply in so that */
  172.                       );                            /* MyDlgHook() can access current FSSpec */
  173.     }                                                            
  174.     while (MyAlert(&reply)==ReplyDialogAgainItem);    /* repeat until user has had enough */
  175. }
  176.  
  177.  
  178.